/**
 * COPYRIGHT HangZhou Trustway Technology Company Limited
 * All right reserved.
 */
package com.robam.cloud.component.componentfeign.initdata;

import com.robam.cloud.common.bean.apicenter.entity.SysConfigWithBLOBs;
import com.robam.cloud.common.bean.restful.entity.RespMsg;
import com.robam.cloud.common.bean.restful.entity.ServiceException;
import com.robam.cloud.component.componentfeign.basefeign.FeignApiCenterBaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * @Description
 * @Author 薛铁琪
 * @CreateTime 2020-07-27 19:44
 * @Version 1.0
 */

@Component
public class SysConfigData {
    @Autowired
    FeignApiCenterBaseUtil feignApiCenterBaseUtil;

    private HashMap<String, SysConfigWithBLOBs> stringSysConfigWithBLOBsHashMap;


    private void init() {
        if (stringSysConfigWithBLOBsHashMap == null || stringSysConfigWithBLOBsHashMap.size() <= 0) {
            //加载stringSysConfigWithBLOBsHashMap
            RespMsg respMsg = feignApiCenterBaseUtil.getAllSysConfig();
            if (respMsg == null) {
                throw new ServiceException("api中心异常请稍后重试");
            }
            if (respMsg.getCode() != 200) {
                throw new ServiceException(respMsg.getMsg());
            }
            List<SysConfigWithBLOBs> sysConfigWithBLOBsList = (List<SysConfigWithBLOBs>) respMsg.getData();
            stringSysConfigWithBLOBsHashMap = new HashMap<>();
            if (sysConfigWithBLOBsList != null && sysConfigWithBLOBsList.size() > 0) {
                for (SysConfigWithBLOBs sysConfigWithBLOBs : sysConfigWithBLOBsList) {
                    stringSysConfigWithBLOBsHashMap.put(sysConfigWithBLOBs.getCsdm(), sysConfigWithBLOBs);
                }
            }
        }
    }

    public SysConfigWithBLOBs getSysConfigByCsz(String csdm) {
        init();
        return stringSysConfigWithBLOBsHashMap.get(csdm);
    }
}
